package day07.ThreadDemo;

/*
 * Author: JacksonCoder
 * FileName: CyclicBarrierDemo
 * Version: 1.0.0
 * Date: 2025/4/14
 */

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(3, () -> {
            System.out.println("All threads reached the barrier. Run together!");
        });

        for (int i = 0; i < 3; i++) {
            int id = i;
            new Thread(() -> {
                System.out.println("Thread " + id + " ready");
                try {
                    barrier.await(); // 等待其他线程
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("Thread " + id + " continues");
            }).start();
        }
    }
}
